package me.zhengjie.modules.equipment.service.impl;

import me.zhengjie.modules.equipment.domain.TSparepart;
import me.zhengjie.modules.equipment.domain.TSparepartRetrieval;
import me.zhengjie.modules.equipment.repository.TSparepartRetrievalRepository;
import me.zhengjie.modules.system.common.EnumSparepartStatus;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.modules.equipment.repository.TSparepartRepository;
import me.zhengjie.modules.equipment.service.TSparepartService;
import me.zhengjie.modules.equipment.service.dto.TSparepartDTO;
import me.zhengjie.modules.equipment.service.dto.TSparepartQueryCriteria;
import me.zhengjie.modules.equipment.service.mapper.TSparepartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;

/**
* @author sw
* @date 2019-07-06
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TSparepartServiceImpl implements TSparepartService {

    @Autowired
    private TSparepartRepository tSparepartRepository;

    @Autowired
    private TSparepartRetrievalRepository tSparepartRetrievalRepository;

    @Autowired
    private TSparepartMapper tSparepartMapper;

    @Override
    public Object queryAll(TSparepartQueryCriteria criteria, Pageable pageable){
        Page<TSparepart> page = tSparepartRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(tSparepartMapper::toDto));
    }

    @Override
    public Object queryAll(TSparepartQueryCriteria criteria){
        return tSparepartMapper.toDto(tSparepartRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public TSparepartDTO findById(Integer id) {
        Optional<TSparepart> tSparepart = tSparepartRepository.findById(id);
        ValidationUtil.isNull(tSparepart,"TSparepart","id",id);
        return tSparepartMapper.toDto(tSparepart.get());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TSparepartDTO create(TSparepart resources) {
        TSparepart sparepart = tSparepartRepository.save(resources);
        TSparepartRetrieval retrieval = new TSparepartRetrieval();

        retrieval.setEqptId(resources.getId());  //备件库
        retrieval.setState(EnumSparepartStatus.INSERT.getValue());
        retrieval.setRetrievalBy(SecurityUtils.getName());
        retrieval.setRetrievalTime(new Timestamp(System.currentTimeMillis()));
        retrieval.setSpareno(sparepart.getSpareno());
        retrieval.setSparepartname(sparepart.getSparepartname());
        retrieval.setTypename(sparepart.getTypename());
        retrieval.setPurpose("入库");
        retrieval.setNotes("入库");
        tSparepartRetrievalRepository.save(retrieval);

        return tSparepartMapper.toDto(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TSparepart resources) {
        Optional<TSparepart> optionalTSparepart = tSparepartRepository.findById(resources.getId());
        ValidationUtil.isNull( optionalTSparepart,"TSparepart","id",resources.getId());

        TSparepart tSparepart = optionalTSparepart.get();
        // 此处需自己修改
        resources.setId(tSparepart.getId());
        tSparepartRepository.save(tSparepartRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) {
        tSparepartRepository.deleteById(id);
    }
}